﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Xml;
using System.Web;

namespace tenpayApp
{
	public static partial class HttpHelper
	{
		/// <summary>
		/// 请求指定网址
		/// </summary>
		/// <param name="_url">网址</param>
		/// <param name="content">post的数据 null的话则使用get方式请求</param>
		/// <param name="cookie">set-cookie的数据</param>
		/// <param name="referer">来源网址</param>
		/// <param name="username">http用户名</param>
		/// <param name="password">http密码</param>
		/// <returns>web返回对象</returns>
		public static HttpWebResponse HttpWebRequest(string _url, string content, string cookie, string referer, string username, string password, IHttpHeaders heads)
		{
			HttpWebResponse response = null;
			try
			{
				HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);

				if ((cookie ?? string.Empty).Length > 0)
				{
					request.CookieContainer = new CookieContainer();
					request.CookieContainer.SetCookies(new Uri(_url), cookie);
				}

				if ((username ?? string.Empty).Length > 0)
				{
					request.Credentials = new System.Net.NetworkCredential(username, password ?? string.Empty);
				}

				if ((referer ?? string.Empty).Length > 0)
				{
					request.Referer = referer;
				}
				
				if(heads != null)
				{
					if(heads.Accept != null)
					{
						request.Accept = heads.Accept;
					}
					if(heads.AcceptCharset != null)
					{
						request.Headers.Add(HttpRequestHeader.AcceptCharset, heads.AcceptCharset);
					}
					if(heads.AcceptLanguage != null)
					{
						request.Headers.Add(HttpRequestHeader.AcceptLanguage, heads.AcceptLanguage);
					}
					if(heads.UserAgent != null)
					{
						request.UserAgent = heads.UserAgent;
					}
				}

				if ((content ?? string.Empty).Length > 0)
				{
					request.Method = "POST";
					request.ContentType = "application/x-www-form-urlencoded";
					byte[] bytes = Encoding.UTF8.GetBytes(content);
					request.ContentLength = bytes.Length;

					Stream stream = request.GetRequestStream();
					stream.Write(bytes, 0, bytes.Length);
					stream.Close();
				}
				
				response = request.GetResponse() as HttpWebResponse;

			}
			catch (Exception ex)
			{
			}
			finally
			{
			}

			return response;
		}

		#region 获取返回的文本
		public static string GetResponse(string _url)
		{
			return GetResponse(_url, null, null, null, Encoding.UTF8);
		}
		
		public static string GetResponse(string _url, Encoding encode)
		{
			return GetResponse(_url, null, null, null, encode);
		}

		public static string GetResponse(string _url, string content, string cookie, string referer)
		{
			return GetResponse(_url, content, cookie, referer, Encoding.UTF8);
		}

		public static string GetResponse(string _url, string content, string cookie, string referer, Encoding encode)
		{
			return GetResponse(_url, content, cookie, referer, encode, null);
		}

		public static string GetResponse(string _url, string content, string cookie, string referer, Encoding encode, IHttpHeaders heads)
		{
			string rv = string.Empty;
			HttpWebResponse response = HttpWebRequest(_url, content, cookie, referer, null, null, heads);
			if (response == null) return rv;
			try
			{
				using (Stream stream = response.GetResponseStream())
				{
					using (StreamReader sr = new StreamReader(stream, encode))
					{
						rv = sr.ReadToEnd();
					}
				}
			}
			catch (Exception ex)
			{

			}
			response.Close();

			return rv;
		}
		#endregion
	}
	
	public class FirefoxHttpHeader : IHttpHeaders
	{
		#region IHttpHeaders 成员

		public string  UserAgent
		{
			get { return "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 QQDownload/1.7"; }
		}

		public string  Accept
		{
			get { return "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; }
		}

		public string  AcceptLanguage
		{
			get { return "zh-cn,zh;q=0.5"; }
		}

		public string  AcceptCharset
		{
			get { return "GB2312,utf-8;q=0.7,*;q=0.7"; }
		}

		#endregion
}
	
	public interface IHttpHeaders
	{
		string UserAgent { get; }
		string Accept { get; }
		string AcceptLanguage { get; }
		string AcceptCharset { get; }
	}
}
